package com.baichuan;



import com.baichuan.download.Downloader;
import com.baichuan.download.MultiThreadFileDownloader;
import com.baichuan.support.MultiThreadDownloadProgressPrinter;
import com.baichuan.utils.FileSizeUtil;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;

public class FastDownloadApplication {



    public static void main(String[] args) throws IOException {

        String fileURL = "https://official-package.wpscdn.cn/wps/download/WPS_Setup_12763.exe";

        MultiThreadDownloadProgressPrinter downloadProgressPrinter = new MultiThreadDownloadProgressPrinter(200);

        //CompletableFuture的runAsync只是简单的异步执行一个线程，但是它将返回一个CompletableFuture，有了这个CompletableFuture，可以重新组装和调配，这是和一个普通Runnable不同之处。
        CompletableFuture.runAsync(() -> {
            while (true) {
                long alreadyDownloadLength = downloadProgressPrinter.getAlreadyDownloadLength();//已经下载的大小
                long contentLength = downloadProgressPrinter.getContentLength();//文件总大小
                System.out.println(contentLength + "  =>  " +
                        alreadyDownloadLength + "  =>  "+
                        (alreadyDownloadLength*100/(contentLength+0D)) + "%");

                if (alreadyDownloadLength != 0 && alreadyDownloadLength > contentLength) {
                    break;
                }
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Downloader downloader = new MultiThreadFileDownloader(200, downloadProgressPrinter);//配置下载实例
        downloader.download(fileURL, "H:\\downloader");//开启下载
        //fileDownloader.multiThreadDownload(fileURL, "/Users/huaan9527/Desktop", 10);
    }


}
